<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;


/**
 * 商铺模型
 */
class OtcShop extends ModelBase
{

    protected $insert = ['create_time'];
    protected $update = ['number_total'];



    /**
     * 连用户表
     */
    public function member(){
        return $this->belongsTo('Member','user_id','id');
    }


    /**
     * 连用户表
     */
    public function wallet(){
        return $this->belongsTo('Wallet','wallet_id','id');
    }

    /**
     * 获取类型信息
     */
    public function getPoundageTypeNameAttr(){
        $arr=['','系统','会员'];
        return lang($arr[$this->poundage_type]);
    }

    /**
     * 获取正面照
     */
    public function getFullFacePhotoAttr(){
       return get_picture_url($this->full_face_photo_id,false);
    }

    /**
     * 获取反面照
     */
    public function getNegativePhotoAttr(){
        return get_picture_url($this->negative_photo_id,false);
    }

    /**
     * 获取正面照
     */
    public function getHandHeldPhotoAttr(){
        return get_picture_url($this->hand_held_photo_id,false);
    }

    /**
     * 获取手续费
     */
    public function getPoundageNumberAttr($value){
        return $value+UserRankingData::where(['shop_id'=>$this->id,'type'=>15])->sum('money');
    }

    /**
     * 获取提现手续费
     */
    public function getTransferPoundageAttr(){
        return  Transfer::where(['shop_id'=>$this->id,'type'=>23])->sum('poundage_number')+0;
    }

    /**
     * 获取提现
     */
    public function getTransferCountAttr(){
        return  Transfer::where(['shop_id'=>$this->id,'type'=>23])->count();
    }

    /**
     * 获取代收订单排队中金额
     */
    public function getSellMoneyAttr(){
        return OtcTrading::where(['shop_id'=>$this->id,'type'=>2,'state'=>1])->sum('surplus_number')+0;
    }

    /**
     * 获取代发订单排队中金额
     */
    public function getBuyMoneyAttr(){
        return OtcTrading::where(['shop_id'=>$this->id,'type'=>1,'state'=>1])->sum('surplus_number')+0;
    }

    /**
     * 获取代收订单手续费
     */
    public function getSellPoundageAttr(){
        return ShopPoundageLog::where(['shop_id'=>$this->id,'type'=>1])->sum('number')+0;
    }
    /**
     * 获取代发订单手续费
     */
    public function getBuyPoundageAttr(){
        return ShopPoundageLog::where(['shop_id'=>$this->id,'type'=>2])->sum('number')+0;
    }

    /**
     * 获取充值金额
     */
    public function getRechargeMoneyAttr(){
        return EthAccountLog::where(['user_id'=>$this->user_id,'type'=>7,'wallet_id'=>$this->wallet_id])->sum('number')+0;
    }
    /**
     * 投诉订单数
     */
    public function getComplaintCountAttr(){
        return OtcFeedback::where(['shop_id'=>$this->id])->count();
    }

    /**
     * 平均代发完成率
     */
    public function getReleaseSuccessProbabilityAttr(){
        $total_count=OtcOrder::where(['user_id'=>$this->user_id,'shop_id'=>$this->id])->count();
        if($total_count==0){
            return 0;
        }
        $success_count=OtcOrder::where(['user_id'=>$this->user_id,'shop_id'=>$this->id,'state'=>2])->count();

        return format_money($success_count/$total_count*100,2);
    }
    /**
     * 平均代收完成率
     */
    public function getReceiveSuccessProbabilityAttr(){
        $total_count=OtcOrder::where(['uid'=>$this->user_id,'shop_id'=>$this->id])->count();
        if($total_count==0){
            return 0;
        }
        $success_count=OtcOrder::where(['uid'=>$this->user_id,'shop_id'=>$this->id,'state'=>2])->count();

        return format_money($success_count/$total_count*100,2);
    }




    /**
     * 平均处理时间
     */
    public function getAverageProcessingAttr(){
        $success_count=OtcOrder::where(['shop_id'=>$this->id,'state'=>2])->count();

        if($success_count==0){
            return 0;
        }

        $total_minute=OtcOrder::where(['shop_id'=>$this->id,'state'=>2])->sum('receive_minute')+0;
        return ceil($total_minute/$success_count);
    }


    /**
     * 获取收款类型数组
     */
    public function getPayArrayAttr(){
        if($this->pay_type){
            $pays='';
            $images=explode(',',$this->pay_type);
            $arr=['','支付宝','微信','银行卡'];
            foreach ($images as $k=>$v){
                if($k==0){
                    $pays.=$arr[$v];
                }else{
                    $pays.=','.$arr[$v];
                }
            }
            return $pays;
        }else{
            return '';
        }
    }
    /**
     * 获取申请状态
     */
    public function getStateTextAttr(){
        $arr=['','待审核','通过审核','未通过审核'];
        return lang($arr[$this->state]);
    }
    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 时间自动完成
     */
    public function getCreateTimeAttr($value)
    {

        return date('Y-m-d H:i:s',$value);
    }
    /**
     * 时间自动完成
     */
    public function setNumberTotalAttr()
    {
        if(isset($this->number_do) && isset($this->cancel_number)){
            return $this->number_do+$this->cancel_number;
        }else{
            $shop=self::get($this->id);
            return  $shop->number_do+$shop->cancel_number;
        }

    }

    /**
     * 时间自动完成
     */
    public function getAdoptTimeAttr($value)
    {
        if($value){
            return date('Y-m-d H:i:s',$value);
        }else{
            return '';
        }


    }
    /***
     * 发放利息奖励
     */

    public function  interest_reward(){
        if(getConfig('otc_remard_pattern')==1){
            //模式1
//            $total=OtcInterestLog::where(['shop_id'=>$this->id])->sum('total');


        }else{
            //模式2
            $start=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
            $BeginDate=strtotime($start);
            $end_data=strtotime(date('Y-m-d 23:59:59', strtotime("$BeginDate +1 month -1 day")));


//            $BeginDate=getTodayStart();
//            $end_data=getTodayEnd();
            //代收金额
            $collection_money=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>2,'state'=>['in',[2,9,10]] ])->sum('total')+0;
//            $collection_money=100000;
            $collection_count=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>2 ,'state'=>['in',[2,9,10]]])->count();
//            $collection_count=100;
            //代发金额
            $substitute_money=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>1,'state'=>['in',[2,9,10]] ])->sum('total')+0;
//            $substitute_money=100000;
            $substitute_count=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>1,'state'=>['in',[2,9,10]] ])->count();
//            $substitute_count=100;


            //申诉订单数
            $feed_count=OtcFeedback::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id])->count();
//            $feed_count=0;

            $reward_list=InterestAllocation::where(['type'=>2,'status'=>1])->order('id asc')->select();
            if(!$reward_list){
                return false;
            }

            $last_id=InterestAllocation::where(['type'=>2,'status'=>1])->order('id desc')->limit(1)->column('id');
            foreach ($reward_list as $k=>$v){
                $collection_money_config=$v['collection_money_total'];
                $collection_count_config=$v['collection_count_total'];
                $substitute_money_config=$v['substitute_money_total'];
                $substitute_count_config=$v['substitute_count_total'];
                $feed_count_config=$v['feed_count_total'];
//                if($this->id==4 && $v['id']==8){
//                    dd($last_id[0]);
//                    d($collection_money>=$collection_money_config);
//                    d($collection_count>=$collection_count_config);
//                    d($substitute_money >= $substitute_money_config);
//                    dd($feed_count<=$feed_count_config );
//                }

                if($collection_money>=$collection_money_config  &&  $collection_count>=$collection_count_config  &&  $substitute_money >= $substitute_money_config  && $substitute_count>=$substitute_count_config   && $feed_count<=$feed_count_config ){

                    $reward=$v['money'];
                    $type=$v['id'];

                    if($v['id']==$last_id[0]){
                        $count1=$collection_count-$reward_list[$k-1]['collection_count_total'];
                        $count2=$substitute_count-$reward_list[$k-1]['substitute_count_total'];
                    }else {
                        $count1=$v['collection_count'];
                        $count2=$v['substitute_count'];

                    }
                    $grant_total=$count1+$count2;
                    if($reward>0 && $grant_total >0 && !OtcInterestLog::get(['type'=>$type,'user_id'=>$this->user_id,'shop_id'=>$this->id,'interest_type'=>2,'create_time'=>['between',[$BeginDate,$end_data]]]) ){

                        $get_number=$grant_total*$reward;
                        $coin_number=$get_number/$this->wallet->parities;
                        $this->number_do+=$coin_number;
                        $this->save();


                        //写入发放记录
                        OtcInterestLog::create([
                            'user_id'=>$this->user_id,
                            'shop_id'=>$this->id,
                            'number'=>$grant_total,
                            'collection_count'=>$count1,
                            'collection_money'=>$count1*$reward,
                            'substitute_money'=>$count2*$reward,
                            'substitute_count'=>$count2,
                            'coin_number'=>$coin_number,
                            'total'=>$get_number,
                            'price'=>$this->wallet->parities,
                            'wallet_id'=>$this->wallet_id,
                            'interest_type'=>2,
                            'type'=>$type,
                            'reward'=>$reward,
                        ]);

                        ShopAccountLog::create([
                            'user_id'=>$this->user_id,
                            'number'=>$get_number,
                            'shop_id'=>$this->id,
                            'remark'=> '每月额外补贴手续费',
                            'is_add'=>1,
                            'type'=>3
                        ]);


                    }

                }else{
                    break;
                }
            }


        }

    }



    /**
     * 预计分红
     */

    public function get_bonus($type){
        $start=date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
        $BeginDate=strtotime($start);
        $end_data=strtotime(date('Y-m-d 23:59:59', strtotime("$BeginDate +1 month -1 day")));
        //代收金额
        $collection_money=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>2,'state'=>['in',[2,9,10]] ])->sum('total')+0;
        $collection_count=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>2 ,'state'=>['in',[2,9,10]]])->count();
        //代发金额
        $substitute_money=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>1,'state'=>['in',[2,9,10]] ])->sum('total')+0;
        $substitute_count=OtcOrder::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id,'otc_type'=>1,'state'=>['in',[2,9,10]] ])->count();
        //申诉订单数
        $feed_count=OtcFeedback::where(['create_time'=>['between',[$BeginDate,$end_data]],'shop_id'=>$this->id])->count();

        $info=InterestAllocation::where(['type'=>2,'status'=>1,'id'=>$type])->find();
        if(!$info){
            return 0;
        }
        $reward=0;
        $last=InterestAllocation::where(['type'=>2,'status'=>1])->order('id desc')->find();
        if($collection_money>=$info['collection_money_total']  &&  $collection_count>=$info['collection_count_total']  &&  $substitute_money >= $info['substitute_money_total']  && $substitute_count>=$info['substitute_count_total']   && $feed_count>=$info['feed_count_total'] ){
            $reward=$info['money'];
            if($last['id']==$info['id']){
                $grant_total=$info['collection_count']+$info['substitute_count'];
            }else{
                $grant_total=$collection_count+$substitute_count-$info['collection_count_total']-$info['substitute_count_total'];
            }

        }
        if($reward>0 && $grant_total >0  ){

            $get_number=$grant_total*$reward;
        }else{
            $get_number=0;
        }
        return $get_number;
    }




}
